<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.go(-1)"/>
    </van-nav-bar>

    <van-form @submit="onSubmit" ref="formRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="rulesList.mobile"
        maxlength="11"
        type="number"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>

      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="rulesList.code"
        maxlength="6"
        type="number"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="5 * 1000"
            format="ss s"
            v-if="countFlag"
            @finish="countFlag = false"
          />

          <van-button
            v-else
            class="send-yzm"
            size="small"
            type="default"
            round
            native-type="button"
            @click="sendYzm"
          >
            发送验证码
          </van-button>
        </template>
      </van-field>

      <div>
        <van-button block type="info" class="login-btn" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'

export default {
  name: 'login-box',
  data() {
    return {
      user: {
        mobile: '18811655888',
        code: '246810'
      },
      rulesList: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3|5|6|7|8|9]\d{9}$/, message: '请检查手机格式' }
        ],
        code: [
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '请输入六位数字' }
        ]
      },
      countFlag: false
    }
  },
  methods: {
    async onSubmit () {
      try {
        this.$toast.loading({
          message: '登录中...',
          duration: 0
        })

        let data = this.user

        const res = await login(data)

        this.$toast.success('成功登录')

        this.$store.commit('setUser', res.data.data)

        // this.$router.go(-1)
        this.$router.back()
      } catch (err) {
        if(err.response.status === 400) {
          this.$toast.fail(err.response.data.message)
        } else {
          this.$toast.fail('请求失败')
        }
      }

    },
    async sendYzm() {
      try {
        await this.$refs.formRef.validate('mobile')
      } catch(err) {
        console.log(err)
      }

      this.countFlag = true

      this.$toast.loading({
        message: '发送验证码',
        duration: 0
      })

      try {
        await sendSms(this.user.mobile)

        this.$toast.success('发送验证码成功')
      } catch(err) {
        if(err.response.status === 429 ) {
          this.$toast.fail(err.response.data.message)
        } else {
          this.$toast.fail('发送验证码失败')
        }
      }

    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }

  .send-yzm{
    display: block;
    width: 160px;
    height: 46px;
    line-height: 46px;
    font-size: 22px;
  }

  .login-btn {
    margin: 53px 28px 0;
    width: 694px;
    background-color: #6db4fb;
    border: 0;
  }
}
</style>
